package com.fypg.module.medical.service.smslog;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.fypg.module.medical.controller.admin.smslog.vo.*;
import com.fypg.module.medical.dal.dataobject.smslog.MedicalSmslogDO;
import com.fypg.framework.common.pojo.PageResult;
import com.fypg.framework.common.util.object.BeanUtils;

import com.fypg.module.medical.dal.mysql.smslog.SmslogMapper;

import static com.fypg.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.fypg.framework.common.util.collection.CollectionUtils.convertList;
import static com.fypg.module.medical.enums.ErrorCodeConstants.*;

/**
 * 短信发送记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SmslogServiceImpl implements SmslogService {

    @Resource
    private SmslogMapper smslogMapper;

    @Override
    public String createSmslog(SmslogSaveReqVO createReqVO) {
        // 插入
        MedicalSmslogDO smslog = BeanUtils.toBean(createReqVO, MedicalSmslogDO.class);
        smslogMapper.insert(smslog);
        // 返回
        return smslog.getId();
    }

    @Override
    public void updateSmslog(SmslogSaveReqVO updateReqVO) {
        // 校验存在
        validateSmslogExists(updateReqVO.getId());
        // 更新
        MedicalSmslogDO updateObj = BeanUtils.toBean(updateReqVO, MedicalSmslogDO.class);
        smslogMapper.updateById(updateObj);
    }

    @Override
    public void deleteSmslog(String id) {
        // 校验存在
        validateSmslogExists(id);
        // 删除
        smslogMapper.deleteById(id);
    }

    @Override
        public void deleteSmslogListByIds(List<String> ids) {
        // 校验存在
        validateSmslogExists(ids);
        // 删除
        smslogMapper.deleteByIds(ids);
        }

    private void validateSmslogExists(List<String> ids) {
        List<MedicalSmslogDO> list = smslogMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(SMSLOG_NOT_EXISTS);
        }
    }

    private void validateSmslogExists(String id) {
        if (smslogMapper.selectById(id) == null) {
            throw exception(SMSLOG_NOT_EXISTS);
        }
    }

    @Override
    public MedicalSmslogDO getSmslog(String id) {
        return smslogMapper.selectById(id);
    }

    @Override
    public PageResult<MedicalSmslogDO> getSmslogPage(SmslogPageReqVO pageReqVO) {
        return smslogMapper.selectPage(pageReqVO);
    }

}
